load("//bazel:mongo_src_rules.bzl", "mongo_cc_library", "mongo_cc_unit_test")

package(default_visibility = ["//visibility:public"])

exports_files([
    "document_source_extension_test.cpp",
    "document_source_extension_optimizable_test.cpp",
    "document_source_extension_expandable_test.cpp",
])

exports_files(
    glob([
        "*.h",
    ]),
)

# Note that this is a standalone library such that it doesn't need to have any server dependencies like the rest of the extension_host target does.
mongo_cc_library(
    name = "extension_operation_metrics_registry",
    srcs = [
        "operation_metrics_registry.cpp",
    ],
    hdrs = [
        "operation_metrics_registry.h",
    ],
    deps = [
        "//src/mongo:base",
        "//src/mongo/db/extension/shared/handle/aggregation_stage",
    ],
)

mongo_cc_library(
    name = "extension_host",
    srcs = [
        "document_source_extension.cpp",
        "document_source_extension_expandable.cpp",
        "document_source_extension_optimizable.cpp",
        "extension_stage.cpp",
    ],
    deps = [
        ":extension_operation_metrics_registry",
        "//src/mongo:base",
        "//src/mongo/db:commands",
        "//src/mongo/db/extension/host/aggregation_stage",
        "//src/mongo/db/extension/host_connector/adapter:host_adapters",
        "//src/mongo/db/extension/host_connector/adapter:host_services_adapter",
        "//src/mongo/db/extension/host_connector/handle:host_handles",
        "//src/mongo/db/extension/shared/handle/aggregation_stage",
        "//src/mongo/db/pipeline",
        "//src/mongo/db/pipeline:desugarer",
    ],
)

mongo_cc_library(
    name = "extension_loader",
    srcs = [
        "load_extension.cpp",
        "load_stub_parsers.cpp",
        "//src/mongo/db/pipeline:document_source_list_extensions.cpp",
    ],
    deps = [
        ":extension_host",
        "//src/mongo:base",
        "//src/mongo/db/query:query_knobs",
        "//src/third_party/yaml-cpp:yaml",
    ],
)

mongo_cc_unit_test(
    name = "extensions_api_test",
    srcs = [
        "load_extension_test.cpp",
        "load_native_vector_search_test.cpp",
    ],
    # Data lists the targets that must be built to generate extension shared libraries, which are
    # loaded in the unit tests.
    data = [
        "//src/mongo/db/extension/test_examples:bar_mongo_extension",
        "//src/mongo/db/extension/test_examples:duplicate_stage_descriptor_bad_extension",
        "//src/mongo/db/extension/test_examples:duplicate_version_bad_extension",
        "//src/mongo/db/extension/test_examples:explain_mongo_extension",
        "//src/mongo/db/extension/test_examples:extension_errors_mongo_extension",
        "//src/mongo/db/extension/test_examples:foo_mongo_extension",
        "//src/mongo/db/extension/test_examples:host_version_fails_bad_extension",
        "//src/mongo/db/extension/test_examples:host_version_succeeds_mongo_extension",
        "//src/mongo/db/extension/test_examples:initialize_version_fails_bad_extension",
        "//src/mongo/db/extension/test_examples:load_highest_compatible_version_mongo_extension",
        "//src/mongo/db/extension/test_examples:load_two_stages_mongo_extension",
        "//src/mongo/db/extension/test_examples:major_version_max_int_bad_extension",
        "//src/mongo/db/extension/test_examples:major_version_too_high_bad_extension",
        "//src/mongo/db/extension/test_examples:major_version_too_low_bad_extension",
        "//src/mongo/db/extension/test_examples:match_topN_mongo_extension",
        "//src/mongo/db/extension/test_examples:minor_version_too_high_bad_extension",
        "//src/mongo/db/extension/test_examples:native_vector_search_mongo_extension",
        "//src/mongo/db/extension/test_examples:no_compatible_version_bad_extension",
        "//src/mongo/db/extension/test_examples:no_symbol_bad_extension",
        "//src/mongo/db/extension/test_examples:null_initialize_function_bad_extension",
        "//src/mongo/db/extension/test_examples:null_mongo_extension_bad_extension",
        "//src/mongo/db/extension/test_examples:null_stage_descriptor_bad_extension",
        "//src/mongo/db/extension/test_examples:parse_options_mongo_extension",
        "//src/mongo/db/extension/test_examples:sharded_execution_serialization_mongo_extension",
        "//src/mongo/db/extension/test_examples:test_options_mongo_extension",
        "//src/mongo/db/extension/test_examples:toaster_mongo_extension",
    ],
    tags = ["mongo_unittest_seventh_group"],
    target_compatible_with = select({
        "//bazel/config:shared_archive_or_link_dynamic": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }) + select({
        "@platforms//os:linux": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":extension_loader",
        "//src/mongo/db/pipeline:aggregation_context_fixture",
        "//src/mongo/unittest",
    ],
)
